home *** CD-ROM | disk | FTP | other *** search
/ Winzipper / Winzipper_ISO.iso / programming / oracle7 7.2 / DB / UTIL72 / SAMPLE4.SQL < prev    next >
Encoding:
Text File  |  1995-05-18  |  3.6 KB  |  113 lines

  1. rem 
  2. rem $Header: sample4.sql 7020100.1 94/09/28 16:39:48 cli Generic<base> $ 
  3. rem 
  4. Rem  Copyright (c) 1991 by Oracle Corporation 
  5. Rem    NAME
  6. Rem      sample4.sql - <one-line expansion of the name>
  7. Rem    DESCRIPTION
  8. Rem      <short description of component this file declares/defines>
  9. Rem    RETURNS
  10. Rem 
  11. Rem    NOTES
  12. Rem      <other useful comments, qualifications, etc.>
  13. Rem    MODIFIED   (MM/DD/YY)
  14. Rem     rvasired   05/12/92 -  Creation 
  15. /*
  16. ** This program modifies the ACCOUNTS table based on instructions
  17. ** stored in the ACTION table.  Each row of the ACTION table 
  18. ** contains an account number to act upon, an action to be taken
  19. ** (insert, update, or delete), an amount  by which to update the
  20. ** account, and a time tag.
  21. **
  22. ** On an insert, if the account already exists, an update is
  23. ** performed instead.  On an update, if the account does not exist,
  24. ** it is created by an insert.  On a delete, if the row does not
  25. ** exist, no action is taken.
  26. **
  27. ** Copyright (c) 1989,1992 by Oracle Corporation
  28. */
  29.  
  30. DECLARE
  31.     CURSOR c1 IS
  32.         SELECT account_id, oper_type, new_value FROM action
  33.         ORDER BY time_tag 
  34.     FOR UPDATE OF status;
  35.  
  36. BEGIN
  37.     FOR acct IN c1 LOOP      -- process each row one at a time
  38.  
  39.     acct.oper_type := upper(acct.oper_type);
  40.  
  41.     /*----------------------------------------*
  42.      * Process an UPDATE.  If the account to  *
  43.      * be updated doesn't exist, create a new *
  44.      * account.                  *
  45.      *----------------------------------------*/
  46.     IF acct.oper_type = 'U' THEN
  47.         UPDATE accounts SET bal = acct.new_value
  48.                 WHERE account_id = acct.account_id;
  49.  
  50.         IF SQL%NOTFOUND THEN  -- account didn't exist.  Create it.
  51.         INSERT INTO accounts
  52.                    VALUES (acct.account_id, acct.new_value);
  53.           UPDATE action SET status = 
  54.             'Update: ID not found. Value inserted.' 
  55.             WHERE CURRENT OF c1; 
  56.         ELSE
  57.         UPDATE action SET status = 'Update: Success.'
  58.             WHERE CURRENT OF c1;
  59.         END IF;
  60.  
  61.     /*--------------------------------------------*
  62.      * Process an INSERT.  If the account already *
  63.      * exists, do an update of the account        *
  64.      * instead.                      *
  65.      *--------------------------------------------*/
  66.     ELSIF acct.oper_type = 'I' THEN
  67.         BEGIN
  68.             INSERT INTO accounts
  69.             VALUES (acct.account_id, acct.new_value);
  70.             UPDATE action set status = 'Insert: Success.'
  71.             WHERE CURRENT OF c1;
  72.  
  73.         EXCEPTION
  74.             WHEN DUP_VAL_ON_INDEX THEN   -- account already exists
  75.             UPDATE accounts SET bal = acct.new_value
  76.                         WHERE account_id = acct.account_id;
  77.                    UPDATE action SET status = 
  78.                         'Insert: Acct exists. Updated instead.'
  79.                 WHERE CURRENT OF c1;
  80.         END;
  81.  
  82.     /*--------------------------------------------*
  83.      * Process a DELETE.  If the account doesn't  *
  84.      * exist, set the status field to say that    *
  85.      * the account wasn't found.              *
  86.      *--------------------------------------------*/
  87.     ELSIF acct.oper_type = 'D' THEN    
  88.         DELETE FROM accounts 
  89.         WHERE account_id = acct.account_id;
  90.  
  91.         IF SQL%NOTFOUND THEN   -- account didn't exist.
  92.             UPDATE action SET status = 'Delete: ID not found.'
  93.             WHERE CURRENT OF c1;
  94.         ELSE
  95.         UPDATE action SET status = 'Delete: Success.'
  96.             WHERE CURRENT OF c1;
  97.         END IF;
  98.  
  99.     /*--------------------------------------------*
  100.      * The requested operation is invalid.        *
  101.      *--------------------------------------------*/
  102.     ELSE    -- oper_type is invalid
  103.         UPDATE action SET status =
  104.                'Invalid operation. No action taken.'
  105.            WHERE CURRENT OF c1;
  106.  
  107.     END IF;
  108.  
  109.     END LOOP;
  110.     COMMIT;
  111. END;
  112. /
  113.